<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-top: 20px"
      @click="addTm"
      >添加</el-button
    >
    <!-- 
      表格
      data:放数据的地方 数组"
      border:加边框
      el-table-column:每一列
      prop:'每一列的内容'
     -->
    <el-table :data="pageList" style="width: 100%; margin: 20px 0" border>
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column label="品牌LOGO" width="width" align="center">
        <template slot-scope="{ row }">
          <img :src="row.logoUrl" style="height: 66px; width: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="222" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="small"
            @click="editTm(row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            @click="deleteTm(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器-->
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 7]"
      :page-size="limit"
      layout="prev, pager, next,->,sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form style="width: 80%" :model="tmForm" :rules="rules" ref="ruleForm">
        <el-form-item label="品牌名称" label-width="82px" prop="tmName">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="92px" prop="logoUrl">
          <!-- 上传图片 action:上传地址 -->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过1MB
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "TradeMark",
  data() {
    return {
      page: 2,
      limit: 3,
      total: 0,
      pageList: [],
      //显示隐藏表单
      dialogFormVisible: false,
      tmForm: {
        logoUrl: "",
        tmName: "",
      },
      //表单验证
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在2到10个字符",
            trigger: "change",
          },
        ],
        logoUrl: [{ required: true, message: "请选择图片" }],
      },
    };
  },
  mounted() {
    this.getPageList();
  },
  methods: {
    async getPageList() {
      const { page, limit } = this;
      let result = await this.$API.tradeMark.reqTradeMarkList(page, limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.pageList = result.data.records;
      }
    },
    //改变当前页
    handleCurrentChange(page) {
      this.page = page;
      this.getPageList();
    },
    //改变一页的数据个数
    handleSizeChange(limit) {
      this.limit = limit;
      this.getPageList();
    },
    //添加一个品牌
    addTm() {
      this.dialogFormVisible = true;
      this.tmForm = { logoUrl: "", tmName: "" };
    },
    //编辑品牌
    editTm(row) {
      this.dialogFormVisible = true;
      //浅拷贝，取消过后还是服务器的数据
      this.tmForm = { ...row };
    },
    //上传图片成功钩子
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },
    //上传图片之前的判断
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpg" || "image/png";
      const isLt2M = file.size / 1024 / 1024 < 1;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 或PNG格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },
    //添加或修改一个商品
    addOrUpdateTm() {
      this.$refs.ruleForm.validate(async (success) => {
        if (success) {
          this.dialogFormVisible = false;
          // 有id发修改请求，没id发添加请求
          if (this.tmForm.id) {
            let result = await this.$API.tradeMark.reqUpdatetm(this.tmForm);
            if (result.code == 200) {
              this.$message({
                type: "success",
                message: "修改品牌成功",
              });
            } 
          } else {
            let result = await this.$API.tradeMark.reqAddtm(this.tmForm);
            if (result.code == 200) {
              this.$message({
                type: "success",
                message: "添加品牌成功",
              });
            } 
          }
          this.getPageList();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除一个商品
    deleteTm(row) {
      this.$confirm(`是否删除${row.tmName}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        // 删除成功的回调
      })
        .then(async() => {
          let result = await this.$API.tradeMark.reqDeletetm(row.id);
          if (result.code == 200) {
            this.getPageList()
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>


    
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>